package org.autumn.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class SeqControl {
    // 控制线程执行顺序
    static ReentrantLock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();
    static boolean hasPrintT2 = false;

    public static void main(String[] args) {
        new Thread(() -> {
            try {
                lock.lock();
                while (!hasPrintT2){
                   condition.await();
                }
                System.out.println(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "t1").start();

        new Thread(() -> {
            try {
                lock.lock();
                hasPrintT2 = true;
                System.out.println(2);
                condition.signal();
            } finally {
                lock.unlock();
            }
        }, "t2").start();
    }
}
